
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>ksnowlv</title>
  <meta name="author" content="律威">

  
  <meta name="description" content="Java下的锁 1.Lock
2.ReentrantLock
3.ReentrantReadWriteLock 可以使用Condition进行线程间的同步. 其类结构如下： ">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://ksnowlv.github.io/blog/page/2">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="ksnowlv" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="//ajax.useso.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.useso.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.useso.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">

  
  <script type="text/javascript">
    var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-39546615-2']);
    // _gaq.push(['_trackPageview']);

    // (function() {
    //   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    //   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    //   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    // })();

    _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src='http://droidyue-tools.qiniudn.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
  </script>


</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">ksnowlv</a></h1>
  
    <h2>回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="http://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:ksnowlv.github.io" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
  <li><a href="/">博客首页</a></li>
  <li><a href="/blog/archives">文章列表</a></li>
  <!-- <li><a href="/about-me">关于我</a></li>
 -->
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div class="blog-index">
  
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/04/28/android-xia-de-suo/">Android下锁</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-04-28T20:45:00+08:00" pubdate data-updated="true">Apr 28<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/04/28/android-xia-de-suo/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>Java下的锁</h3>

<ul>
<li>1.Lock</li>
<li>2.ReentrantLock</li>
<li>3.ReentrantReadWriteLock</li>
</ul>


<p> 可以使用<strong>Condition</strong>进行线程间的同步.</p>

<p>其类结构如下：
<img src="/images/post/2015-04-28-android-xia-suo/java_lock_classes.png" alt="image" /></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/04/16/test/">Android开源网络库</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-04-16T20:48:00+08:00" pubdate data-updated="true">Apr 16<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/04/16/test/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>目前比较流行的Android网络库</h3>

<ul>
<li><a href="https://android.googlesource.com/platform/frameworks/volley">volley</a></li>
<li><a href="https://github.com/loopj/android-async-http">android-async-http</a></li>
<li><a href="https://github.com/square/retrofit">retrofit</a></li>
<li><a href="https://github.com/square/okhttp">okhttp(支持spdy)</a></li>
<li><a href="https://github.com/androidquery/androidquery">androidquery</a></li>
<li><a href="https://github.com/koush/AndroidAsync">AndroidAsync</a></li>
<li><a href="https://github.com/litesuits/android-lite-http">LiteHttp</a></li>
</ul>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/04/16/mac-xia-ssh-keychang-yong-cao-zuo-ming-ling/">Mac下ssh-key常用操作命令</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-04-16T19:54:00+08:00" pubdate data-updated="true">Apr 16<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/04/16/mac-xia-ssh-keychang-yong-cao-zuo-ming-ling/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><strong>Mac</strong>上关于<strong>ssh key</strong>常用的操作命令</p>

<h3>1.生成公钥/私钥</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'> <span class="n">ssh</span><span class="o">-</span><span class="n">keygen</span> <span class="o">-</span><span class="n">t</span> <span class="n">rsa</span> <span class="o">-</span><span class="n">C</span> <span class="s">&quot;邮箱名&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h3>2.添加公钥/私钥</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">ssh</span><span class="o">-</span><span class="n">add</span> <span class="err">密钥名</span>
</span></code></pre></td></tr></table></div></figure>


<h3>3.显示公钥</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">cat</span> <span class="err">公钥名</span>
</span></code></pre></td></tr></table></div></figure>


<h3>截图如下</h3>

<p><img src="/images/post/2015-04-16-mac-xia-ssh-keychang-yong-cao-zuo-ming-ling/overview.png" alt="image" /></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/03/24/pmd-cha-jian-de-shi-yong/">PMD插件的使用</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-03-24T18:42:00+08:00" pubdate data-updated="true">Mar 24<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/03/24/pmd-cha-jian-de-shi-yong/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><strong>PMD是</strong>一款静态代码分析工具，它能够自动检测各种潜在缺陷以及不安全或未优化的代码，更多地是集中在预先检测缺陷上</p>

<p><strong>Checkstyle</strong>之类的其它工具可以检查代码是否遵循了约定和标准。</p>

<h3>1.下载插件。</h3>

<p><img src="/images/post/2015-03-24-pmd-cha-jian-de-shi-yong/pmd_1.jpg" alt="image" /></p>

<h3>2.选择工程后，右键弹出菜单运行PMD</h3>

<p><img src="/images/post/2015-03-24-pmd-cha-jian-de-shi-yong/pmd_2.jpg" alt="image" /></p>

<h3>3.代码检查的分类情况</h3>

<p><img src="/images/post/2015-03-24-pmd-cha-jian-de-shi-yong/pmd_3.jpg" alt="image" /></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/01/06/ios-64wei-gua-pei/">iOS-64位适配</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-01-06T21:31:00+08:00" pubdate data-updated="true">Jan 6<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/01/06/ios-64wei-gua-pei/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>请参考苹果文档链接：<a href="https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html">https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html</a></p>

<p>摘取几条如下：</p>

<h3>指针不要转换为整数</h3>

<p>   将指针类型转换成整型的情况一般很少,但是非要用指针类型的话,请确保你的变量地址足够大。</p>

<p>   在 32 位中,这段代码运行正常,是因为 int 类型和 指针类型大小相同。
   在 64 位中指针类型是比int类型的大,所以这个赋值将会丢失指针类型的数据。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="kt">int</span> <span class="o">*</span><span class="n">c</span> <span class="o">=</span> <span class="n">something</span> <span class="n">passed</span> <span class="k">in</span> <span class="n">as</span> <span class="n">an</span> <span class="n">argument</span><span class="p">....</span>
</span><span class='line'><span class="kt">int</span> <span class="o">*</span><span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="p">)((</span><span class="kt">int</span><span class="p">)</span><span class="n">c</span> <span class="o">+</span> <span class="mi">4</span><span class="p">);</span> <span class="c1">// Incorrect.</span>
</span><span class='line'><span class="kt">int</span> <span class="o">*</span><span class="n">d</span> <span class="o">=</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">// Correct!</span>
</span></code></pre></td></tr></table></div></figure>


<h3>数据类型保持一致</h3>

<p>   许多常见的程序错误都是应为数据类型用的不一致性导致的,尽管当你用了不 一样的数据类型的时候编译器给出了警告,如果你留意这些改变的话,它将会帮助你意识到这些问题。
   方法调用的时候,方法接受的参数应该和调用的参数类型匹配。如果调用方法的参数比接收的方法的参数要大,这个值将会被截取。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">NSInteger</span> <span class="nf">PerformCalculation</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
</span><span class='line'><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="n">PerformCalculation</span><span class="p">();</span> <span class="c1">// incorrect</span>
</span><span class='line'><span class="n">NSInteger</span> <span class="n">y</span> <span class="o">=</span> <span class="n">PerformCalculation</span><span class="p">();</span> <span class="c1">// correct!</span>
</span></code></pre></td></tr></table></div></figure>


<h3>使用方法函数的原型调度Objective-C的消息</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="nf">doSomething:</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">x</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
</span><span class='line'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">doSomethingElse</span> <span class="p">{</span>
</span><span class='line'>    <span class="kt">int</span> <span class="p">(</span><span class="o">*</span><span class="n">action</span><span class="p">)(</span><span class="kt">id</span><span class="p">,</span> <span class="kt">SEL</span><span class="p">,</span> <span class="kt">int</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="p">(</span><span class="o">*</span><span class="p">)(</span><span class="kt">id</span><span class="p">,</span> <span class="kt">SEL</span><span class="p">,</span> <span class="kt">int</span><span class="p">))</span> <span class="n">objc_msgSend</span><span class="p">;</span>
</span><span class='line'>    <span class="n">action</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="k">@selector</span><span class="p">(</span><span class="nl">doSomething:</span><span class="p">),</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h3>检查可变参数函数</h3>

<p>如果你的方法使用了可变参数如fun(id self, SEL cmd, &hellip;)或者va_start, va_list, va_arg等宏，在iphone6真机测试此函数接收的参数是否正确。</p>

<h3>isa指针不能直接访问</h3>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/01/04/nian-wei-li-zhi/">年尾离职</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2015-01-04T21:25:00+08:00" pubdate data-updated="true">Jan 4<span>th</span>, 2015</time>
        
         | <a href="/blog/2015/01/04/nian-wei-li-zhi/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>2014年过去了，我已经站在了2015的起点。</p>

<p>站在2014年的尾巴上，我做了一个选择，离开淘宝。</p>

<p>在淘宝的9个月的光阴里，出差七个半月的光景。与老婆聚少离多的日子，深感对不起我的她。</p>

<p>工作方面，对其中波澜，也习以为常，尽心尽力去做就好。</p>

<p>年终奖，股票，统统放下，嗷嗷叫地向前走吧。</p>

<p>祝福自己，感谢老婆！祝愿家人在新的一年里开心快乐！</p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/12/03/zu-zhi-wen-jian-bei-ituneshe-icloudtong-bu/">阻止文件被iTunes和iCloud同步</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-12-03T21:48:00+08:00" pubdate data-updated="true">Dec 3<span>rd</span>, 2014</time>
        
         | <a href="/blog/2014/12/03/zu-zhi-wen-jian-bei-ituneshe-icloudtong-bu/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>1.参考官方文档<a href="https://developer.apple.com/library/ios/#qa/qa1719/_index.html">How do I prevent files from being backed up to iCloud and iTunes?</a></h3>

<h3>2.示例代码</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">+</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">addSkipBackupAttributeToItemAtPath:</span><span class="p">(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">path</span> <span class="p">{</span>
</span><span class='line'>  <span class="kt">BOOL</span> <span class="n">success</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filePath</span> <span class="o">=</span> <span class="p">[</span><span class="n">path</span> <span class="n">fileSystemRepresentation</span><span class="p">];</span>
</span><span class='line'>  <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">attrName</span> <span class="o">=</span> <span class="s">&quot;com.apple.MobileBackup&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">void</span> <span class="o">*</span><span class="n">check</span> <span class="o">=</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">NSURLIsExcludedFromBackupKey</span><span class="p">;</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">check</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// First try and remove the extended attribute if it is present</span>
</span><span class='line'>    <span class="kt">int</span> <span class="n">result</span> <span class="o">=</span> <span class="n">getxattr</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">attrName</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">u_int8_t</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">result</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">// The attribute exists, we need to remove it</span>
</span><span class='line'>      <span class="kt">int</span> <span class="n">removeResult</span> <span class="o">=</span> <span class="n">removexattr</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">attrName</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="n">removeResult</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;Removed extended attribute on item at path %@&quot;</span><span class="p">,</span> <span class="n">path</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="n">NSURL</span> <span class="o">*</span><span class="n">url</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSURL</span> <span class="nl">fileURLWithPath:</span><span class="n">path</span><span class="p">];</span>
</span><span class='line'>    <span class="n">success</span> <span class="o">=</span> <span class="p">[</span><span class="n">url</span> <span class="nl">setResourceValue:</span><span class="p">[</span><span class="n">NSNumber</span> <span class="nl">numberWithBool:</span><span class="n">YES</span><span class="p">]</span>
</span><span class='line'>                             <span class="nl">forKey:</span><span class="n">NSURLIsExcludedFromBackupKey</span>
</span><span class='line'>                              <span class="nl">error:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// iOS 5.0.1 and lower</span>
</span><span class='line'>    <span class="n">u_int8_t</span> <span class="n">attrValue</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class='line'>    <span class="kt">int</span> <span class="n">result</span> <span class="o">=</span>
</span><span class='line'>        <span class="n">setxattr</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">attrName</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">attrValue</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">attrValue</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>    <span class="n">success</span> <span class="o">=</span> <span class="p">(</span><span class="n">result</span> <span class="o">==</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;Add skip backup attribute for item at path: %@, result: %d&quot;</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span>
</span><span class='line'>        <span class="n">success</span><span class="p">);</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">success</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<pre><code>   通过上述方法可以使该目录和该目录包含的所有文件和文件夹不被icloud和itunes同步了！
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/12/02/zlibyu-gzip-jie-ya-suo-dui-bi/">ZLib与GZip解压缩对比</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-12-02T20:51:00+08:00" pubdate data-updated="true">Dec 2<span>nd</span>, 2014</time>
        
         | <a href="/blog/2014/12/02/zlibyu-gzip-jie-ya-suo-dui-bi/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>1.ZLib与GZip解压缩</h3>

<pre><code>ZLib库默认解压缩方法，并非GZip解压缩，而是ZLib的。
zlib(RFC1950):一种格式，是对deflate进行了简单的封装； 
gzip(RFC1952):一种格式，也是对deflate进行的封装.
gzip与GZip是一种数据压缩格式，可以大体分为头部，数据部和尾部三个部分
gzip数据头比zlib数据头要大，因为它保存了文件名和其他文件系统信息，事实上这是广泛使用的gzip文件的数据头格式。
而zlib和gzip格式的区别仅仅是头部和尾部不一样，而实际的内容都是deflate编码的，即： gzip = gzip头(10字节) + deflate编码的实际内容 + gzip尾(8字节)
</code></pre>

<h3>ZLib头部</h3>

<p>0x78,0x9c 是zlib数据头(非固定，参考RFC 1951)。或者0x0,0x0,0xFF,0xFF也可以实现正常解压缩，而00 00 FF FF是zlib容错方式的数据块头</p>

<h4>GZip头部：</h4>

<p>第一个字节0x1f(31),第二个字节为0x8b(139),标识为GZIP格式。第三个字节为0x08(8)，指示DEFLATE方法.目前只有一种，CM=8;</p>

<h4>GZip尾部：</h4>

<p>CRC32：4字节。原始(未压缩)数据的32位校验和。
ISIZE：4字节。原始(未压缩)数据的长度的低32位。
GZIP中字节排列顺序是LSB方式，即Little-Endian，与ZLIB中的相反</p>

<h3>2.objective-c版本的ZLib与GZip解压缩</h3>

<h4>ZLib压缩</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">+</span> <span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nf">zlibCompressData:</span><span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nv">sourceData</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSUInteger</span> <span class="n">sourceDataLength</span> <span class="o">=</span> <span class="p">[</span><span class="n">sourceData</span> <span class="n">length</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">sourceDataLength</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">sourceData</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">z_stream</span> <span class="n">stream</span><span class="p">;</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">z_stream</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">next_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">Bytef</span> <span class="o">*</span><span class="p">)[</span><span class="n">sourceData</span> <span class="n">bytes</span><span class="p">];</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">avail_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">uInt</span><span class="p">)</span><span class="n">sourceDataLength</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">total_out</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zalloc</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zfree</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">deflateInit</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="n">Z_DEFAULT_COMPRESSION</span><span class="p">)</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">const</span> <span class="kt">int</span> <span class="n">KBufLen</span> <span class="o">=</span> <span class="mi">1024</span><span class="p">;</span>
</span><span class='line'>  <span class="n">Byte</span> <span class="n">buf</span><span class="p">[</span><span class="n">KBufLen</span><span class="p">];</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">BOOL</span> <span class="n">isCompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSMutableData</span> <span class="o">*</span><span class="n">compressedData</span> <span class="o">=</span>
</span><span class='line'>      <span class="p">[</span><span class="n">NSMutableData</span> <span class="nl">dataWithLength:</span><span class="n">sourceDataLength</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">compressedData</span> <span class="nl">setData:</span><span class="nb">nil</span><span class="p">];</span> <span class="c1">//必须得加</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">while</span> <span class="p">(</span><span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">=</span> <span class="n">KBufLen</span><span class="p">;</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">next_out</span> <span class="o">=</span> <span class="n">buf</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">res</span> <span class="o">=</span> <span class="n">deflate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="n">Z_FINISH</span><span class="p">);</span>
</span><span class='line'>    <span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">switch</span> <span class="p">(</span><span class="n">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_NEED_DICT:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_DATA_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_MEM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_STREAM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_BUF_ERROR:</span> <span class="p">{</span>
</span><span class='line'>      <span class="n">isCompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">default</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">==</span> <span class="n">Z_OK</span> <span class="o">||</span> <span class="n">res</span> <span class="o">==</span> <span class="n">Z_STREAM_END</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">const</span> <span class="kt">int</span> <span class="n">dataLen</span> <span class="o">=</span> <span class="n">KBufLen</span> <span class="o">-</span> <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span><span class="p">;</span>
</span><span class='line'>        <span class="n">isCompressOK</span> <span class="o">=</span> <span class="n">YES</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="n">dataLen</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="p">[</span><span class="n">compressedData</span> <span class="nl">appendBytes:</span><span class="n">buf</span> <span class="nl">length:</span><span class="n">dataLen</span><span class="p">];</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">isCompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">res</span> <span class="o">=</span> <span class="n">deflateEnd</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">);</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">isCompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">compressedData</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>ZLib解压</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">+</span> <span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nf">zlibUncompressData:</span><span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nv">sourceData</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSUInteger</span> <span class="n">sourceDataLength</span> <span class="o">=</span> <span class="p">[</span><span class="n">sourceData</span> <span class="n">length</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">sourceDataLength</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">sourceData</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">z_stream</span> <span class="n">stream</span><span class="p">;</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">z_stream</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">next_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">Bytef</span> <span class="o">*</span><span class="p">)[</span><span class="n">sourceData</span> <span class="n">bytes</span><span class="p">];</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">avail_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">uInt</span><span class="p">)</span><span class="n">sourceDataLength</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">total_out</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zalloc</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zfree</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="n">inflateInit</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">);</span>
</span><span class='line'>  <span class="c1">// inflateInit2(&amp;strm, (15+32))</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">const</span> <span class="kt">int</span> <span class="n">KBufLen</span> <span class="o">=</span> <span class="mi">1024</span><span class="p">;</span>
</span><span class='line'>  <span class="n">Byte</span> <span class="n">buf</span><span class="p">[</span><span class="n">KBufLen</span><span class="p">];</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">BOOL</span> <span class="n">isUncompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSMutableData</span> <span class="o">*</span><span class="n">decompressed</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSMutableData</span> <span class="nl">dataWithLength:</span><span class="n">sourceDataLength</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">decompressed</span> <span class="nl">setData:</span><span class="nb">nil</span><span class="p">];</span> <span class="c1">//必须得加</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">while</span> <span class="p">(</span><span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">=</span> <span class="n">KBufLen</span><span class="p">;</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">next_out</span> <span class="o">=</span> <span class="n">buf</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">res</span> <span class="o">=</span> <span class="n">inflate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="n">Z_NO_FLUSH</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">switch</span> <span class="p">(</span><span class="n">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_NEED_DICT:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_DATA_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_MEM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_STREAM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_BUF_ERROR:</span> <span class="p">{</span>
</span><span class='line'>      <span class="n">isUncompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">default</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">==</span> <span class="n">Z_OK</span> <span class="o">||</span> <span class="n">res</span> <span class="o">==</span> <span class="n">Z_STREAM_END</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">const</span> <span class="kt">int</span> <span class="n">dataLen</span> <span class="o">=</span> <span class="n">KBufLen</span> <span class="o">-</span> <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span><span class="p">;</span>
</span><span class='line'>        <span class="n">isUncompressOK</span> <span class="o">=</span> <span class="n">YES</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="n">dataLen</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="p">[</span><span class="n">decompressed</span> <span class="nl">appendBytes:</span><span class="n">buf</span> <span class="nl">length:</span><span class="n">dataLen</span><span class="p">];</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">isUncompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">inflateEnd</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">)</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">isUncompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">decompressed</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>GZip压缩</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">+</span> <span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nf">gzipCompress:</span><span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nv">sourceData</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSUInteger</span> <span class="n">sourceDataLength</span> <span class="o">=</span> <span class="p">[</span><span class="n">sourceData</span> <span class="n">length</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">sourceDataLength</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">sourceData</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">z_stream</span> <span class="n">stream</span><span class="p">;</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">z_stream</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">next_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">Bytef</span> <span class="o">*</span><span class="p">)[</span><span class="n">sourceData</span> <span class="n">bytes</span><span class="p">];</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">avail_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">uInt</span><span class="p">)</span><span class="n">sourceDataLength</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">total_out</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zalloc</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zfree</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">//只有设置为MAX_WBITS + 16才能在在压缩文本中带header和trailer</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">deflateInit2</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="n">Z_DEFAULT_COMPRESSION</span><span class="p">,</span> <span class="n">Z_DEFLATED</span><span class="p">,</span> <span class="n">MAX_WBITS</span> <span class="o">+</span> <span class="mi">16</span><span class="p">,</span>
</span><span class='line'>                   <span class="mi">8</span><span class="p">,</span> <span class="n">Z_DEFAULT_STRATEGY</span><span class="p">)</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">const</span> <span class="kt">int</span> <span class="n">KBufLen</span> <span class="o">=</span> <span class="mi">1024</span><span class="p">;</span>
</span><span class='line'>  <span class="n">Byte</span> <span class="n">buf</span><span class="p">[</span><span class="n">KBufLen</span><span class="p">];</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">BOOL</span> <span class="n">isCompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSMutableData</span> <span class="o">*</span><span class="n">compressedData</span> <span class="o">=</span>
</span><span class='line'>      <span class="p">[</span><span class="n">NSMutableData</span> <span class="nl">dataWithLength:</span><span class="n">sourceDataLength</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">compressedData</span> <span class="nl">setData:</span><span class="nb">nil</span><span class="p">];</span> <span class="c1">//必须得加</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">while</span> <span class="p">(</span><span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">=</span> <span class="n">KBufLen</span><span class="p">;</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">next_out</span> <span class="o">=</span> <span class="n">buf</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">res</span> <span class="o">=</span> <span class="n">deflate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="n">Z_FINISH</span><span class="p">);</span>
</span><span class='line'>    <span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">switch</span> <span class="p">(</span><span class="n">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_NEED_DICT:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_DATA_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_MEM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_STREAM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_BUF_ERROR:</span> <span class="p">{</span>
</span><span class='line'>      <span class="n">isCompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">default</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">==</span> <span class="n">Z_OK</span> <span class="o">||</span> <span class="n">res</span> <span class="o">==</span> <span class="n">Z_STREAM_END</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">const</span> <span class="kt">int</span> <span class="n">dataLen</span> <span class="o">=</span> <span class="n">KBufLen</span> <span class="o">-</span> <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span><span class="p">;</span>
</span><span class='line'>        <span class="n">isCompressOK</span> <span class="o">=</span> <span class="n">YES</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="n">dataLen</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="p">[</span><span class="n">compressedData</span> <span class="nl">appendBytes:</span><span class="n">buf</span> <span class="nl">length:</span><span class="n">dataLen</span><span class="p">];</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">isCompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">res</span> <span class="o">=</span> <span class="n">deflateEnd</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">);</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">isCompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">compressedData</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>GZip解压</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">+</span> <span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nf">gzipUncompress:</span><span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="p">)</span><span class="nv">sourceData</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSUInteger</span> <span class="n">sourceDataLength</span> <span class="o">=</span> <span class="p">[</span><span class="n">sourceData</span> <span class="n">length</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">sourceDataLength</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">sourceData</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">z_stream</span> <span class="n">stream</span><span class="p">;</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">z_stream</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">next_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">Bytef</span> <span class="o">*</span><span class="p">)[</span><span class="n">sourceData</span> <span class="n">bytes</span><span class="p">];</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">avail_in</span> <span class="o">=</span> <span class="p">(</span><span class="n">uInt</span><span class="p">)</span><span class="n">sourceDataLength</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">total_out</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zalloc</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>  <span class="n">stream</span><span class="p">.</span><span class="n">zfree</span> <span class="o">=</span> <span class="n">Z_NULL</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="n">inflateInit2</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="n">MAX_WBITS</span> <span class="o">+</span> <span class="mi">16</span><span class="p">);</span>
</span><span class='line'>  <span class="c1">// inflateInit2(&amp;strm, (15+32))</span>
</span><span class='line'>  <span class="c1">//只有设置为MAX_WBITS + 16才能在解压带header和trailer的文本</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">const</span> <span class="kt">int</span> <span class="n">KBufLen</span> <span class="o">=</span> <span class="mi">1024</span><span class="p">;</span>
</span><span class='line'>  <span class="n">Byte</span> <span class="n">buf</span><span class="p">[</span><span class="n">KBufLen</span><span class="p">];</span>
</span><span class='line'>  <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">BOOL</span> <span class="n">isUncompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSMutableData</span> <span class="o">*</span><span class="n">decompressed</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSMutableData</span> <span class="nl">dataWithLength:</span><span class="n">sourceDataLength</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">decompressed</span> <span class="nl">setData:</span><span class="nb">nil</span><span class="p">];</span> <span class="c1">//必须得加</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">while</span> <span class="p">(</span><span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">KBufLen</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Byte</span><span class="p">));</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span> <span class="o">=</span> <span class="n">KBufLen</span><span class="p">;</span>
</span><span class='line'>    <span class="n">stream</span><span class="p">.</span><span class="n">next_out</span> <span class="o">=</span> <span class="n">buf</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">res</span> <span class="o">=</span> <span class="n">inflate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">,</span> <span class="n">Z_SYNC_FLUSH</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">switch</span> <span class="p">(</span><span class="n">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_NEED_DICT:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_DATA_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_MEM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_STREAM_ERROR:</span>
</span><span class='line'>    <span class="k">case</span> <span class="nl">Z_BUF_ERROR:</span> <span class="p">{</span>
</span><span class='line'>      <span class="n">isUncompressOK</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">default</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">==</span> <span class="n">Z_OK</span> <span class="o">||</span> <span class="n">res</span> <span class="o">==</span> <span class="n">Z_STREAM_END</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">const</span> <span class="kt">int</span> <span class="n">dataLen</span> <span class="o">=</span> <span class="n">KBufLen</span> <span class="o">-</span> <span class="n">stream</span><span class="p">.</span><span class="n">avail_out</span><span class="p">;</span>
</span><span class='line'>        <span class="n">isUncompressOK</span> <span class="o">=</span> <span class="n">YES</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="n">dataLen</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="p">[</span><span class="n">decompressed</span> <span class="nl">appendBytes:</span><span class="n">buf</span> <span class="nl">length:</span><span class="n">dataLen</span><span class="p">];</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">isUncompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">break</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">inflateEnd</span><span class="p">(</span><span class="o">&amp;</span><span class="n">stream</span><span class="p">)</span> <span class="o">!=</span> <span class="n">Z_OK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">isUncompressOK</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">decompressed</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h3>3.代码示例</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">NSString</span><span class="o">*</span> <span class="n">hello</span> <span class="o">=</span> <span class="s">@&quot;helloabc123&quot;</span><span class="p">;</span>
</span><span class='line'>    <span class="n">NSData</span> <span class="o">*</span><span class="n">helloData</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSData</span> <span class="nl">dataWithBytes:</span><span class="n">hello</span><span class="p">.</span><span class="n">UTF8String</span> <span class="nl">length:</span><span class="p">[</span><span class="n">hello</span> <span class="nl">lengthOfBytesUsingEncoding:</span><span class="n">NSUTF8StringEncoding</span><span class="p">]];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSData</span> <span class="o">*</span><span class="n">compressData</span> <span class="o">=</span> <span class="p">[</span><span class="n">CommonFuctions</span> <span class="nl">zlibCompressData:</span><span class="n">helloData</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSData</span> <span class="o">*</span><span class="n">unCompressData</span> <span class="o">=</span> <span class="p">[</span><span class="n">CommonFuctions</span> <span class="nl">zlibUncompressData:</span><span class="n">compressData</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSString</span><span class="o">*</span> <span class="n">string</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSString</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithData:</span><span class="n">unCompressData</span> <span class="nl">encoding:</span><span class="n">NSUTF8StringEncoding</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;Zlib compressData Length = %lu compressData = %@&quot;</span><span class="p">,</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">long</span><span class="p">)</span><span class="n">compressData</span><span class="p">.</span><span class="n">length</span><span class="p">,[</span><span class="n">compressData</span> <span class="n">description</span><span class="p">]);</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSData</span> <span class="o">*</span><span class="n">compressDataGZip</span> <span class="o">=</span> <span class="p">[</span><span class="n">CommonFuctions</span> <span class="nl">gzipCompress:</span><span class="n">helloData</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSData</span> <span class="o">*</span><span class="n">uncompressDataGZip</span> <span class="o">=</span> <span class="p">[</span><span class="n">CommonFuctions</span> <span class="nl">gzipUncompress:</span><span class="n">compressDataGZip</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSString</span><span class="o">*</span> <span class="n">stringRes</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSString</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithData:</span><span class="n">uncompressDataGZip</span> <span class="nl">encoding:</span><span class="n">NSUTF8StringEncoding</span><span class="p">];;</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;GZip compressData Length = %lu compressData = %@&quot;</span><span class="p">,</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">long</span><span class="p">)</span><span class="n">compressDataGZip</span><span class="p">.</span><span class="n">length</span><span class="p">,[</span><span class="n">compressDataGZip</span> <span class="n">description</span><span class="p">]);</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<h3>4.日志输出：</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="mi">2014</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mo">02</span> <span class="mi">21</span><span class="o">:</span><span class="mi">14</span><span class="o">:</span><span class="mf">20.946</span> <span class="n">ZlibGZipTest</span><span class="p">[</span><span class="mi">14417</span><span class="o">:</span><span class="mi">4245959</span><span class="p">]</span> <span class="n">Zlib</span> <span class="n">compressData</span> <span class="n">Length</span> <span class="o">=</span> <span class="mi">19</span> <span class="n">compressData</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mi">789</span><span class="n">ccb48</span> <span class="n">cdc9c94f</span> <span class="mi">4</span><span class="n">c4a3634</span> <span class="mi">32060019</span> <span class="mi">9003</span><span class="n">d1</span><span class="o">&gt;</span>
</span><span class='line'><span class="mi">2014</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mo">02</span> <span class="mi">21</span><span class="o">:</span><span class="mi">14</span><span class="o">:</span><span class="mf">24.142</span> <span class="n">ZlibGZipTest</span><span class="p">[</span><span class="mi">14417</span><span class="o">:</span><span class="mi">4245959</span><span class="p">]</span> <span class="n">GZip</span> <span class="n">compressData</span> <span class="n">Length</span> <span class="o">=</span> <span class="mi">31</span> <span class="n">compressData</span> <span class="o">=</span> <span class="o">&lt;</span><span class="mf">1f</span><span class="mi">8</span><span class="n">b0800</span> <span class="mo">00000000</span> <span class="mo">0003</span><span class="n">cb48</span> <span class="n">cdc9c94f</span> <span class="mi">4</span><span class="n">c4a3634</span> <span class="mi">32060012</span> <span class="n">e873880b</span> <span class="mo">000000</span><span class="o">&gt;</span>
</span><span class='line'><span class="p">(</span><span class="n">lldb</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<h3>5.结论</h3>

<h4>1.zlib默认压缩后比gzip小一点。</h4>

<h4>2.zlib与gzip头尾对比。</h4>

<ul>
<li><strong>zlib头</strong>：789ccb48  <strong>zlib尾</strong>：32060019 9003d1</li>
<li><strong>gzip头</strong>：1f8b0800 00000000 0003cb48 <strong>gzip尾</strong>：32060012 e873880b 000000</li>
</ul>


<p>两者相比差12个字节。</p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/11/20/ios8-zhong-de-spdy-shi-yong/">iOS8中的spdy使用</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-11-20T10:56:00+08:00" pubdate data-updated="true">Nov 20<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/11/20/ios8-zhong-de-spdy-shi-yong/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>spdy需要客户端和服务端同时支持。</h3>

<h4>1.spdy available on both OS X Yosemite and iOS 8</h4>

<p>SPDY/2, SPDY/3, and SPDY/3.1 are supported Supported transparently by NSURLSession No source changes needed—it just works</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'> <span class="n">NSURL</span> <span class="o">*</span><span class="n">url</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSURL</span> <span class="nl">URLWithString:</span><span class="s">@&quot;https://www.example.com/&quot;</span><span class="p">];</span>
</span><span class='line'>  <span class="n">NSURLSessionDataTask</span> <span class="o">*</span><span class="n">task</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSURLSession</span> <span class="n">sharedSession</span><span class="p">]</span>
</span><span class='line'>        <span class="nl">dataTaskWithURL:</span><span class="n">url</span>
</span><span class='line'>      <span class="nl">completionHandler:</span><span class="o">^</span><span class="p">(</span><span class="n">NSData</span> <span class="o">*</span><span class="n">data</span><span class="p">,</span> <span class="n">NSURLResponse</span> <span class="o">*</span><span class="n">response</span><span class="p">,</span>
</span><span class='line'>                          <span class="n">NSError</span> <span class="o">*</span><span class="n">error</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>   <span class="p">}];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">task</span> <span class="n">resume</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>直接复用session,启用spdy.</p>

<h4>2.SPDY does require server-side support<em> Client negotiates with server during TLS handshake</em> Uses <a href="https://">https://</a> URLs<em> Existing web server software and many CDNs already support SPDY Will not interfere with your NSURLProtocol subclasses</em> Apps might have their own implementation of SPDY or other protocols</h4>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/11/18/ios-shi-yong-openssl/">iOS使用OpenSSL</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-11-18T17:18:00+08:00" pubdate data-updated="true">Nov 18<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/11/18/ios-shi-yong-openssl/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><code>iOS</code>中<code>OpenSSL</code></p>

<pre><code>If your app depends on OpenSSL,you should compile OpenSSL yourself 
and statically link a known version of OpenSSL into your app. 
This use of OpenSSL is possible on both OS X and iOS. However, 
unless you are trying to maintain source compatibility with an existing open source project, 
you should generally use a different API.
</code></pre>

<p>苹果建议采用系统原生的</p>

<pre><code>Common Crypto and Security Transforms are the recommended alternatives for general encryption.
 CFNetwork and Secure Transport are the recommended alternatives for secure communications.
</code></pre>

<p>如果你必须使用<code>OpenSSL</code>，可以直接源代码的形式或库的形式引入Xcode工程中。
参考<a href="https://github.com/x2on/OpenSSL-for-iPhone">OpenSSL-for-iPhone</a></p>
</div>
  
  


    </article>
  
  <div class="pagination">
    
      <a class="prev" href="/blog/page/3/">&larr; Older</a>
    
    <a href="/blog/archives">Blog Archives</a>
    
    <a class="next" href="/">Newer &rarr;</a>
    
  </div>
</div>
<aside class="sidebar">
  
    <section>
  <h1>关于我</h1>
  <p>律威,QQ:309235009 微信:ksnowlv</p>
  <p>手机单机游戏/手机网游封神online/手机阅读/腾讯iOS地图客户端&实时公交&开放API(地图&街景)/iPad/iPhone淘宝客户端及性能优化</p>
  <p>目前在银客做银客/简理财/&#8230;</p>
  <p>能做一点,是一点;不求大成,不求一万年;积少成多,水到渠成!</p>
</section>
<section>
  <h1>最近发布</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2017/04/26/android-drawable/">Android drawable</a>
      </li>
    
      <li class="post">
        <a href="/blog/2017/04/22/iOS-xia-shi-yong-ffmpeg/">iOS下使用ffmpeg</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/ios-you-xian-hou-tai-ren-wu/">iOS有限后台任务</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/android-xia-jie-ya-suo/">Android下gzip数据流解压缩</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/facebook-infer-de-shi-yong/">FaceBook-infer的使用</a>
      </li>
    
  </ul>
</section>
<section>
  <h1>分类</h1>
    <ul id="category-list"><li><a href='/blog/categories/android-jie-ya-suo/'>Android-解压缩 (1)</a></li><li><a href='/blog/categories/androidduo-xian-cheng/'>Android多线程 (3)</a></li><li><a href='/blog/categories/androidcha-jian/'>Android插件 (1)</a></li><li><a href='/blog/categories/androidhui-tu-./'>Android绘图。 (1)</a></li><li><a href='/blog/categories/androidwang-luo/'>Android网络 (1)</a></li><li><a href='/blog/categories/git/'>git (6)</a></li><li><a href='/blog/categories/githubbo-ke/'>github博客 (8)</a></li><li><a href='/blog/categories/ios/'>iOS (6)</a></li><li><a href='/blog/categories/ios-block/'>iOS-block (1)</a></li><li><a href='/blog/categories/ios-bug/'>iOS-Bug (2)</a></li><li><a href='/blog/categories/ios-memory/'>iOS-Memory (4)</a></li><li><a href='/blog/categories/ios-push/'>iOS-push (1)</a></li><li><a href='/blog/categories/ios-ui/'>iOS-UI (2)</a></li><li><a href='/blog/categories/ios-you-hua/'>iOS-优化 (1)</a></li><li><a href='/blog/categories/ios-nei-cun/'>iOS-内存 (1)</a></li><li><a href='/blog/categories/ios-ji-chu/'>iOS-基础 (3)</a></li><li><a href='/blog/categories/ios-duo-xian-cheng/'>iOS-多线程 (3)</a></li><li><a href='/blog/categories/ios-cun-chu/'>iOS-存储 (1)</a></li><li><a href='/blog/categories/ios-an-quan/'>iOS-安全 (1)</a></li><li><a href='/blog/categories/ios-wang-luo/'>iOS-网络 (13)</a></li><li><a href='/blog/categories/ios-jie-ya-suo/'>iOS-解压缩 (1)</a></li><li><a href='/blog/categories/ios-she-ji-mo-shi/'>iOS-设计模式 (1)</a></li><li><a href='/blog/categories/ios-diao-shi/'>iOS-调试 (1)</a></li><li><a href='/blog/categories/iosyou-hua/'>iOS优化 (1)</a></li><li><a href='/blog/categories/iosji-chu/'>iOS基础 (2)</a></li><li><a href='/blog/categories/iosyin-pin/'>iOS音频 (1)</a></li><li><a href='/blog/categories/mac/'>Mac (4)</a></li><li><a href='/blog/categories/mac-duo-xian-cheng/'>Mac-多线程 (1)</a></li><li><a href='/blog/categories/nginx/'>nginx (3)</a></li><li><a href='/blog/categories/php/'>php (3)</a></li><li><a href='/blog/categories/python/'>python (12)</a></li><li><a href='/blog/categories/swift/'>swift (11)</a></li><li><a href='/blog/categories/uml/'>UML (8)</a></li><li><a href='/blog/categories/xcode/'>Xcode (11)</a></li><li><a href='/blog/categories/chan-pin-she-ji/'>产品设计 (2)</a></li><li><a href='/blog/categories/gong-ju/'>工具 (2)</a></li><li><a href='/blog/categories/shu-ju-ku/'>数据库 (2)</a></li><li><a href='/blog/categories/sheng-huo-pian-duan/'>生活片段 (10)</a></li><li><a href='/blog/categories/zhi-ye-sheng-ya/'>职业生涯 (7)</a></li><li><a href='/blog/categories/du-shu/'>读书 (2)</a></li></ul>
</section>
<section>
  <h1>外部链接</h1>
    <ul id="外部链接">
    	<li><a href="https://developer.apple.com">苹果开发者中心</a> </li>
    	<li><a href="http://stackoverflow.com/questions/tagged/ios">stackoverflow</a></li>
    	<li><a href="http://ksnowlv.blog.163.com/blog/#m=0">ksnowlv网易博客</a></li>
	</ul>
</section>

  
</aside>

    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 律威 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>

  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253538110'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/z_stat.php%3Fid%3D1253538110%26online%3D1' type='text/javascript'%3E%3C/script%3E"));</script>

  <script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F4c1727a94ea20751730e8a556dbdbf82' type='text/javascript'%3E%3C/script%3E"));
</script>

</p>



</footer>
  

<script type="text/javascript">
      var disqus_shortname = 'ksnowlv';
      
        
        var disqus_script = 'count.js';
      
    (function () {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    }());
</script>








</body>
</html>
